//
//  TBFrameworkHandler.m
//  Unity-iPhone
//
//  Created by YLo. on 13-8-1.
//
//

#import "TBFrameworkHandler.h"
#define TargetObject "TBUnity"  /*需要通知对象（U3D）*/
#define TargetMethod "ReceiveMessage" /*通知对象的方法*/

#ifndef TargetObject
#error 需要设置发送对象
#endif
#ifndef TargetMethod
#error 需要设置处理方法
#endif

#define _Send_Msg_(methodName,msg)     UnitySendMessage(TargetObject, methodName, msg) /*向U3D发送消息*/

/*****************************************/
#if defined(__cplusplus)
extern "C" {
#endif
#pragma mark - SDK 平台接口调用
void XCTBInit(){
    [[NSNotificationCenter defaultCenter] removeObserver:[TBFrameworkHandler sharedHandler]
                                                    name:kTBUserLogoutNotification
                                                  object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:[TBFrameworkHandler sharedHandler]
                                                    name:kTBLeavePlatformNotification
                                                  object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:[TBFrameworkHandler sharedHandler]
                                                    name:kTBInitDidFinishNotification
                                                  object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:[TBFrameworkHandler sharedHandler]
                                             selector:@selector(TBLeavedPlatform:)
                                                 name:kTBLeavePlatformNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:[TBFrameworkHandler sharedHandler]
                                             selector:@selector(TBDidLogout:)
                                                 name:kTBUserLogoutNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:[TBFrameworkHandler sharedHandler]
                                             selector:@selector(TBInitDidFinish:)
                                                 name:kTBInitDidFinishNotification
                                               object:nil];
#warning 根据游戏具体需求设置自行修改该方法实现
    [[TBPlatform defaultPlatform] TBInitPlatformWithAppID:100000
                                        screenOrientation:UIInterfaceOrientationLandscapeLeft
                          isContinueWhenCheckUpdateFailed:NO];


}

void XCShowToolBar(int place,bool isUsingOldPlace){
    [[TBPlatform defaultPlatform] TBShowToolBar:(TBToolBarPlace)place isUseOldPlace:isUsingOldPlace];
}

void XCHideToolBar(){
    [[TBPlatform defaultPlatform] TBHideToolBar];
}

void XCLogin(int tag){
    [[NSNotificationCenter defaultCenter] removeObserver:[TBFrameworkHandler sharedHandler]
                                                    name:kTBLoginNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:[TBFrameworkHandler sharedHandler]
                                             selector:@selector(TBLoginResult:)
                                                 name:kTBLoginNotification object:nil];
    [[TBPlatform defaultPlatform] TBLogin:0];
    _Send_Msg_("ReceiveMessage","TBLogin");
}
void XCLogout(){
    _Send_Msg_("ReceiveMessage","TBLogout");
    [[TBPlatform defaultPlatform] TBLogout:1];
}
void XCSwitchAccount(){
    _Send_Msg_("ReceiveMessage","TBSwitchAccount");
    [[TBPlatform defaultPlatform] TBSwitchAccount];
}
void XCIsLogined(){
    BOOL isLogined  = [[TBPlatform defaultPlatform] TBIsLogined];
    NSString *smsg  = [NSString stringWithFormat:@"IsLogined:%d",isLogined];
    const char *msg = smsg.UTF8String;
    _Send_Msg_("ReceiveMessage",msg);
}
void XCSetDebug(){
    [[TBPlatform defaultPlatform] TBSetUpdateDebugMode:0];
    _Send_Msg_("ReceiveMessage","Debug Mode On");
}
void XCSetUseOldLoadingMode(BOOL isUse){
    [[TBPlatform defaultPlatform] TBSetUseOldLoadingMode:isUse];
}
    
void XCSessionID(){
    NSString *sessionid = [TBPlatform defaultPlatform].sessionID;
    NSString *msg       = [NSString stringWithFormat:@"SessionID:%@",sessionid];
    _Send_Msg_("ReceiveMessage",msg.UTF8String);
}
void XCUserID(){
    NSString *userid = [TBPlatform defaultPlatform].userID;
    NSString *msg    = [NSString stringWithFormat:@"UserID:%@",userid];
    _Send_Msg_("ReceiveMessage",msg.UTF8String);
}
void XCNickName(){
    NSString *nickName = [TBPlatform defaultPlatform].nickName;
    NSString *msg      = [NSString stringWithFormat:@"NickName:%@",nickName];
    _Send_Msg_("ReceiveMessage",msg.UTF8String);
}
//void XCCheckUpdate(){
//    [[TBPlatform defaultPlatform] TBAppVersionUpdate:0 delegate:[TBFrameworkHandler sharedHandler]];
//    _Send_Msg_("TBCheckUpdate");
//}
void XCPayRMB(int amount,const char *order,const char *payDes){
    [[TBPlatform defaultPlatform] TBUniPayForCoin:[NSString stringWithUTF8String:order]
                                       needPayRMB:amount
                                   payDescription:[NSString stringWithUTF8String:payDes]
                                         delegate:[TBFrameworkHandler sharedHandler]];
    _Send_Msg_("ReceiveMessage","TBUnipayRMB");
}
void XCExchange(const char *order,const char *payDes){
    [[TBPlatform defaultPlatform] TBUniPayForCoin:[NSString stringWithUTF8String:order]
                                   payDescription:[NSString stringWithUTF8String:payDes]];
    _Send_Msg_("ReceiveMessage","TBExchange");
}
void XCCheckOrder(const char * order){
    [[TBPlatform defaultPlatform] TBCheckPaySuccess:@"orderhere"
                                           delegate:[TBFrameworkHandler sharedHandler]];
    _Send_Msg_("ReceiveMessage","CheckOrder");
}
void XCUserCenter(int tag){
    [[TBPlatform defaultPlatform] TBEnterUserCenter:0];
    _Send_Msg_("ReceiveMessage","EnterUserCenter");
}
void XCGameRecommend(int tag){
    [[TBPlatform defaultPlatform] TBEnterAppCenter:0];
    _Send_Msg_("ReceiveMessage","GameRecommend");
}
void XCEnterBBS(int tag){
    int res = [[TBPlatform defaultPlatform] TBEnterAppBBS:0];
    NSString *msg = [NSString stringWithFormat:@"EnterBBS:%d",res];
    _Send_Msg_("ReceiveMessage",msg.UTF8String);
}
#if defined(__cplusplus)
}
#endif
#pragma mark - *****************************************************************
static TBFrameworkHandler *p_instance = NULL;
@implementation TBFrameworkHandler
- (id)init{
    if (self = [super init]) {

    }
    return self;
}
+ (TBFrameworkHandler*)sharedHandler{
    if(!p_instance){
        p_instance = [[TBFrameworkHandler alloc] init];
    }
    return p_instance;
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

//
//#pragma mark - 检查更新结果回调
//- (void)appVersionUpdateDidFinish:(TB_APP_UPDATE_RESULT)updateResult{
//    NSString *msg = [NSString stringWithFormat:@"CheckUpdateResult:%d",updateResult];
//    _Send_Msg_(msg.UTF8String);
//}
/*****************************************/
#pragma mark - 购买物品成功回调
- (void)TBBuyGoodsDidSuccessWithOrder:(NSString *)order{
    NSString *msg = [NSString stringWithFormat:@"BuyGoodsSuccess:%@",order];
    _Send_Msg_("ReceiveMessage",msg.UTF8String);
}
- (void)TBBuyGoodsDidFailedWithOrder:(NSString *)order resultCode:(TB_BUYGOODS_ERROR)errorType{
    NSString *msg = [NSString stringWithFormat:@"BuyGoodsFailedWithCode:%d",errorType];
    _Send_Msg_("ReceiveMessage",msg.UTF8String);
}
- (void)TBBuyGoodsDidStartRechargeWithOrder:(NSString *)order{
    NSString *msg = [NSString stringWithFormat:@"BuyGoodsDidEnterRechargeWebView"];
    _Send_Msg_("ReceiveMessage",msg.UTF8String);
}
- (void)TBBuyGoodsDidCancelByUser:(NSString *)order{
    NSString *msg = [NSString stringWithFormat:@"BuyGoodsDidCancelByUser"];
    _Send_Msg_("ReceiveMessage",msg.UTF8String);
}
/*****************************************/
#pragma mark - 查询订单结果回调
//- (void)TBCheckOrderSuccessWithResult:(NSDictionary *)dict{
//    _Send_Msg_("CheckOrderSucessWithDictionaryResult");
//}
- (void)TBCheckOrderFinishedWithOrder:(NSString *)orderString
                               amount:(int)amount
                               status:(TBCheckOrderStatusType)statusType{
    NSString *msg = [NSString stringWithFormat:
                     @"CheckOrderResult:{order:%@,amount:%d,status:%d}",
                     orderString,amount,statusType];
    _Send_Msg_("ReceiveMessage",msg.UTF8String);
}
- (void)TBCheckOrderDidFailed:(NSString *)order{
    _Send_Msg_("ReceiveMessage","CheckOrderFailed");
}
/*****************************************/
#pragma mark - 消息通知处理方法
//登录结果通知
- (void)TBLoginResult:(NSNotification *)notify{
    NSString *msg = [NSString stringWithFormat:@"LoginResult:%d",[[TBPlatform defaultPlatform] TBIsLogined]];
    _Send_Msg_("ReceiveMessage",msg.UTF8String);
}
//注销通知
- (void)TBDidLogout:(NSNotification *)notify{
    _Send_Msg_("ReceiveMessage","DidLogout");
}
//离开平台通知
- (void)TBLeavedPlatform:(NSNotification *)notify{
    NSString *msg = [NSString stringWithFormat:@"LeavePlatformWithType:%@",
                     [notify.userInfo objectForKey:TBLeavedPlatformTypeKey]];
    _Send_Msg_("ReceiveMessage",msg.UTF8String);
}
//初始化完成
- (void)TBInitDidFinish:(NSNotification *)notify{
    _Send_Msg_("ReceiveMessage","TBInitPlatforDone");
    XCLogin(0);
}
@end
